import Bean.Book;
import Bean.BookService;
import net.sf.json.JSONArray;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import static net.sf.json.JSONArray.fromObject;

@WebServlet("/BookServlet")
public class BookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idStr = (String)request.getParameter("id");
        int id = idStr == null ? 1 : Integer.parseInt(idStr);
        List<Book> books = new BookService().getBookByCategory(id);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        for(Book x : books)
        System.out.println(x);



        out.println(JSONArray.fromObject(books));
    }
}
